<?php
/**
 * 后台专辑管理控制器。
 *
 * @author    Fairyin <fairyin@126.com>
 * @copyright © 2016 www.imcn.vip
 * @version   v1.0
 */

namespace App\Http\Controllers\Shadow;

use View;
use Config;
use Response;
use Illuminate\Http\Request;
use App\Http\Model\Detail;

class PhotoDetailController extends ShadowController
{
    public function onGet($id)
    {
        $this->results['token'] = csrf_token();
        $this->results['id'] = $id;
        $o_lists = Detail::where('status', '=', 'show')
            ->where('pa_id', '=', $id)->get();
        $this->results['count'] = count($o_lists) + 1;
        $a_crossranges = $a_endwises = array();
        foreach ($o_lists as $vv) {
            if ($vv->photo_type == 'crossrange') {
                $a_crossranges[] = $vv;
            } else {
                $a_endwises[] = $vv;
            }
        }
        $this->results['crossranges'] = $a_crossranges;
        $this->results['endwises'] = $a_endwises;
        
        return View::make('shadow/album_detail', $this->results);
    }
    
    public function onPost(Request $request, $id)
    {
        $s_qianzhui = $request->get('qianzhui', '');
        $s_houzhui = $request->get('houzhui', '');
        $i_start = $request->get('start', 1);
        $i_end = $request->get('end', 1);
        $s_type = $request->get('type', '');
        $a_tyes = json_decode($s_type, true);
        if ($i_end < 1) {
            return $this->ajaxError(array('reason' => '保存失败！'));
        }
        for ($ii = $i_start; $ii <= $i_end; $ii++) { 
            $o_detail = new Detail();
            $o_detail->pa_id = $id;
            $o_detail->resource = $s_qianzhui . $ii . $s_houzhui;
            $s_photo_type = 'endwise';
            if (isset($a_tyes[$ii]) && $a_tyes[$ii] != 'endwise') {
                $s_photo_type = $a_tyes[$ii];
            }
            $o_detail->photo_type = $s_photo_type;
            $o_detail->save();
        }
        
        return $this->ajaxSuccess(array('reason' => '保存成功！'));
    }
}
